<?php

/**
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
 * presentations documents.
 *
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
 * General Public License version 3 as published by the Free Software Foundation.
 *
 * For the full copyright and license information, please read the LICENSE
 * file that was distributed with this source code. For the full list of
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
 *
 * @see        https://github.com/PHPOffice/PHPPresentation
 *
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
 */

declare(strict_types=1);

namespace PhpOffice\PhpPresentation\Tests\Writer\PowerPoint2007;

use PhpOffice\PhpPresentation\Exception\FileNotFoundException;
use PhpOffice\PhpPresentation\Shape\Drawing;
use PhpOffice\PhpPresentation\Tests\PhpPresentationTestCase;

/**
 * Test class for PhpOffice\PhpPresentation\Writer\PowerPoint2007\PptMedia.
 *
 * @coversDefaultClass \PhpOffice\PhpPresentation\Writer\PowerPoint2007\PptMedia
 */
class PptMediaTest extends PhpPresentationTestCase
{
    protected $writerName = 'PowerPoint2007';

    public function testDrawing(): void
    {
        $oSlide = $this->oPresentation->getActiveSlide();
        $oShape = $oSlide->createDrawingShape();
        $oShape->setPath(PHPPRESENTATION_TESTS_BASE_DIR . '/resources/images/PhpPresentationLogo.png');

        $this->assertZipFileExists('ppt/media/' . $oShape->getIndexedFilename());
        $this->assertIsSchemaECMA376Valid();
    }

    public function testDrawingException(): void
    {
        $path = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/images/filedoesntexist.png';

        $this->expectException(FileNotFoundException::class);
        $this->expectExceptionMessage(sprintf(
            'The file "%s" doesn\'t exist',
            $path
        ));

        $oSlide = $this->oPresentation->getActiveSlide();
        $oShape = $oSlide->createDrawingShape();
        $oShape->setPath($path, false);

        $this->writePresentationFile($this->oPresentation, 'PowerPoint2007');
    }

    public function testDrawingZip(): void
    {
        $oSlide = $this->oPresentation->getActiveSlide();
        $oDrawing = new Drawing\ZipFile();
        $oDrawing->setPath('zip://' . PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'Sample_01_Simple.pptx#ppt/media/phppowerpoint_logo1.gif');
        $oSlide->addShape($oDrawing);

        $this->assertZipFileExists('ppt/media/' . $oDrawing->getIndexedFilename());
        $this->assertIsSchemaECMA376Valid();
    }

    public function testDrawingZipException(): void
    {
        $path = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/images/filedoesntexist.png';

        $this->expectException(FileNotFoundException::class);
        $this->expectExceptionMessage(sprintf(
            'The file "%s" doesn\'t exist',
            $path
        ));

        $oDrawing = new Drawing\ZipFile();
        $oDrawing->setPath('zip://' . $path . '#secondpath');

        $oSlide = $this->oPresentation->getActiveSlide();
        $oSlide->addShape($oDrawing);

        $this->writePresentationFile($this->oPresentation, 'PowerPoint2007');
    }

    public function testDrawingBase64(): void
    {
        $oSlide = $this->oPresentation->getActiveSlide();
        $oShape = new Drawing\Base64();
        $oShape->setData('');
        $oSlide->addShape($oShape);

        $this->assertZipFileExists('ppt/media/' . $oShape->getIndexedFilename());
        $this->assertIsSchemaECMA376Valid();
    }

    public function testMemoryDrawing(): void
    {
        $gdImage = imagecreatetruecolor(140, 20);
        $textColor = imagecolorallocate($gdImage, 255, 255, 255);
        imagestring($gdImage, 1, 5, 5, 'Created with PHPPresentation', $textColor);

        $oShape = new Drawing\Gd();
        $oShape->setImageResource($gdImage);

        $oSlide = $this->oPresentation->getActiveSlide();
        $oSlide->addShape($oShape);

        $this->assertZipFileExists('ppt/media/' . $oShape->getIndexedFilename());
        $this->assertIsSchemaECMA376Valid();
    }
}
